<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">

<head>
    <meta charset="UTF-8">
    <title>终端列表</title>
    <meta name="ctx" th:content="${#httpServletRequest.getContextPath()}"/>
    <link rel="stylesheet" th:href="@{/css/admin/base-v2.css}">
    <link rel="stylesheet" th:href="@{/lib/flatpickr/flatpickr.min.css}">
    <link rel="stylesheet" th:href="@{/lib/font-awesome/4.7.0/css/font-awesome.min.css}">
    <link rel="stylesheet" th:href="@{/css/admin/app.css}">
</head>

<body>
<div class="app-page">
    <div class="app-page-content">
        <div class="app-card">
            <div class="app-card-body">
                <div class="app-card-body-con">
                    <!-- 过滤器 -->
                    <form class="filter">

                        <div class="filter-item form-item row">
                            <div class="label"><label>终端序列号</label></div>
                            <div class="flex1">
                                <input type="text" placeholder="终端序列号" id="deviceCode" name="deviceCode">
                            </div>
                        </div>
                        <div class="filter-item form-item row">
                            <div class="label"><label>MAC</label></div>
                            <div class="flex1">
                                <input type="text" placeholder="MAC" id="deviceMac" name="deviceMac">
                            </div>
                        </div>
                        <div class="filter-item form-item row">
                            <div class="label"><label>终端名</label></div>
                            <div class="flex1">
                                <input type="text" placeholder="终端名" id="deviceName" name="deviceName">
                            </div>
                        </div>
                        <div class="filter-item form-item row">
                            <div class="label"><label>在线状态</label></div>
                            <div class="flex1">
                                <select id="deviceStatus" name="deviceStatus">
                                    <option value="">全部</option>
                                    <option value="0">在线</option>
                                    <option value="1">离线</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-item row">
                            <div class="filter-btn z-df">
                                <button class="z-btn z-primary" onclick="queryDeviceInfo()">查询</button>
                            </div>
                        </div>

                    </form>
                </div>
            </div>
        </div>

        <div class="app-card">
            <div class="app-card-body">
                <div class="app-card-body-con">

                    <div class="z-btn-group">
                        <button shiro:hasPermission="P_B_device_change_group" data-state="5"
                                class="j-btn-control z-btn z-default">移动分组
                        </button>
                        <button shiro:hasPermission="P_B_device_remote_shutdown" data-state="4"
                                class="j-btn-control z-btn z-default">远程关机
                        </button>
                        <button shiro:hasPermission="P_B_device_time_switch_machine" data-state="3"
                                class="j-btn-control z-btn z-default">定时开关机
                        </button>
                        <button shiro:hasPermission="P_B_device_time_switch_screen" data-state="2"
                                class="j-btn-control z-btn z-default">定时开关屏
                        </button>
                        <button shiro:hasPermission="P_B_device_reboot" data-state="1"
                                class="j-btn-control z-btn z-default">重启终端
                        </button>
                        <button shiro:hasPermission="P_B_device_volume_setting" data-state="0"
                                class="j-btn-control z-btn z-default">音量设置
                        </button>
                        <button shiro:hasPermission="P_B_device_untie" data-state="6"
                                class="j-btn-control z-btn z-default">解绑终端
                        </button>
                    </div>

                    <div class="container z-df">
                        <div class="left-sidebar device-group">
                            <div class="body">
                                <div class="head">
                                    <div class="btn-group z-flex">
                                        <div shiro:hasPermission="P_B_device_group_add" class="btn btn-add">
                                            <i class="fa fa-plus"></i>
                                        </div>
                                        <div shiro:hasPermission="P_B_device_group_edit" class="btn btn-edit">
                                            <i class="fa fa-pencil"></i>
                                        </div>
                                        <div shiro:hasPermission="P_B_device_group_delete" class="btn btn-del">
                                            <i class="fa fa-trash"></i>
                                        </div>
                                    </div>
                                    <!--<div class="handle z-dropdown">-->
                                    <!--<span class="dropdown-title">操作选项</span>-->
                                    <!--<ul>-->
                                    <!--<li>-->
                                    <!--<a onclick="openShutdown()">远程关机</a>-->
                                    <!--<a onclick="openRestart()">重启</a>-->
                                    <!--<a onclick="openDevicePower()">设置开关机</a>-->
                                    <!--<a onclick="openDeviceScreenPower()">设置开关屏</a>-->
                                    <!--<a onclick="openDeviceSound()">音量</a>-->
                                    <!--</li>-->
                                    <!--</ul>-->
                                    <!--</div>-->
                                </div>
                                <ul id="tree" class="ztree"></ul>
                            </div>
                        </div>


                        <table class="z-table">
                            <thead>
                            <th>
                                <input type="checkbox" class="input j-checkbox-all">
                            </th>
                            <th>终端序列号</th>
                            <th>设备mac</th>
                            <th>终端名</th>
                            <th>设备IP</th>
                            <th>在线状态</th>
                            <th>机型</th>
                            <th>mips版本</th>
                            <th>固件版本</th>
                            </thead>
                            <tbody th:each="resultList : ${resultList}">
                            <tr th:attr="data-device-code=${resultList.deviceNum},
	                                         data-device-mac=${resultList.deviceMac}">
                                <td><input type="checkbox" class="input j-checkbox"></td>
                                <td>
                                    <shiro:hasPermission name="P_B_device_detail">
                                        <a th:href="'detail?deviceCode='+ ${resultList.deviceNum}" class="layer-open"
                                           data-title="终端详情">
                                    </shiro:hasPermission>
                                    <span th:text="${resultList.deviceNum}"></span>
                                    <shiro:hasPermission name="P_B_device_detail">
                                        </a>
                                    </shiro:hasPermission>
                                </td>
                                <td th:text="${resultList.deviceMac}"></td>
                                <td th:text="${resultList.deviceName}"></td>
                                <td th:text="${resultList.deviceIp}"></td>
                                <td th:text="${resultList.deviceStatusDesc}"></td>
                                <td th:text="${resultList.deviceModel}"></td>
                                <td th:text="${resultList.deviceSoftVer}"></td>
                                <td th:text="${resultList.deviceFirmwareVer}"></td>

                            </tr>
                            </tbody>
                        </table>

                    </div>

                    <!-- 分页信息 -->
                    <div class="app-card-body">
                        <!-- 前端分页 -->
                        <div class="pagination z-pat z-fr"></div>
                    </div>

                </div>
            </div>
        </div>

        <!-- 新增 -->
        <div class="popup-default z-popup z-fixed z-df z-flex-cc z-hide">
            <div class="bg" data-popup-close=""></div>
            <!-- 新增分组 -->
            <div class="popup-group-add section z-hide">
                <div class="title">新增终端分组</div>
                <div class="content">
                    <form>
                        <div class="form-item row">
                            <div class="label">
                                <label>父级组名</label>
                            </div>
                            <div class="body">
                                <input type="hidden" id="parentId" name="parentId" disabled>
                                <input type="text" id="parentName" name="parentName" disabled>
                            </div>
                        </div>
                        <div class="form-item row">
                            <div class="label">
                                <label>分组名称</label>
                            </div>
                            <div class="body">
                                <input type="text" id="groupName" name="groupName">
                            </div>
                        </div>
                        <div class="form-item row">
                            <div class="label">
                                <label>分组编码</label>
                            </div>
                            <div class="body">
                                <input type="text" id="groupCode" name="groupCode">
                            </div>
                        </div>
                    </form>
                    <div class="z-btn-group">
                        <a class="z-btn z-primary" onclick="saveDeviceGroup()">确定</a>
                        <a class="z-btn z-default" data-popup-close="">取消</a>
                    </div>
                </div>
            </div>
            <!-- 分组编辑 -->
            <div class="popup-group-edit section z-hide">
                <div class="title">编辑分组信息</div>
                <div class="content">
                    <form class="z-form z-align">
                        <div class="form-item row">
                            <div class="label">
                                <label>分组名称</label>
                            </div>
                            <div class="body">
                                <input type="hidden" id="deviceGroupIdEdit" name="deviceGroupIdEdit">
                                <input type="hidden" id="parentIdEdit" name="parentIdEdit">
                                <input type="text" id="groupNameEdit" name="groupNameEdit">
                            </div>
                        </div>
                        <div class="form-item row">
                            <div class="label">
                                <label>分组编码</label>
                            </div>
                            <div class="body">
                                <input type="text" id="groupCodeEdit" name="groupCodeEdit">
                            </div>
                        </div>
                    </form>
                    <div class="z-btn-group">
                        <a class="z-btn z-primary" onclick="updateDeviceGroup()">确定</a>
                        <a class="z-btn z-default" data-popup-close="">取消</a>
                    </div>
                </div>
            </div>
        </div>
    </div>


    <!-- 设备操作 -->
    <div class="popup-default z-popup z-fixed z-df z-flex-cc z-hide">
        <div class="bg" data-popup-close></div>
        <!-- 音量设置 -->
        <div class="popup-device-sound section z-hide">
            <div class="title">音量设置</div>
            <div class="content">
                <form>
                    <input type="hidden" name="deviceCodeSound" id="deviceCodeSound">
                    <input type="hidden" name="groupCodeSound" id="groupCodeSound">
                    <div class="form-item row">
                        <div class="label">
                            <label>音量值</label>
                        </div>
                        <div class="body">
                            <input type="text" min="0" max="100" oninput="checkVolume(this)" name="deviceVolume" id="deviceVolume" value="">
                            0~100
                        </div>
                    </div>
                </form>
                <div class="z-btn-group">
                    <a class="z-btn z-primary" onclick="setVolume()">确定</a>
                    <a class="z-btn z-default" data-popup-close>取消</a>
                </div>
            </div>
        </div>
        <!-- 电源 -->
        <div class="popup-device-power section z-hide">
            <div class="title">开关机设置</div>
            <div class="content">
                <form>
                    <input type="hidden" name="deviceCodePower">
                    <div class="form-item row">
                        <div class="label">
                            <label>开机时间</label>
                        </div>
                        <div class="body">
                            <input type="datetime" name="bootTimePower" class="ft-hour" value="00:00">
                        </div>
                    </div>
                    <div class="form-item row">
                        <div class="label">
                            <label>关机时间</label>
                        </div>
                        <div class="body">
                            <input type="datetime" name="offTimePower" class="ft-hour" value="00:00">
                        </div>
                    </div>
                </form>
                <div class="z-btn-group">
                    <a class="z-btn z-primary" onclick="setTimerShutDownOrStart()">确定</a>
                    <a class="z-btn z-default" data-popup-close>取消</a>
                </div>
            </div>
        </div>
        <!-- 定时开关屏幕 -->
        <div class="popup-screen-power section z-hide">
            <div class="title">开关屏设置</div>
            <div class="content">
                <form>
                    <input type="hidden" name="deviceCodePower">
                    <div class="form-item row">
                        <div class="label">
                            <label>开屏时间</label>
                        </div>
                        <div class="body">
                            <input type="datetime" name="bootTimePower" class="ft-hour" value="00:00">
                        </div>
                    </div>
                    <div class="form-item row">
                        <div class="label">
                            <label>关屏时间</label>
                        </div>
                        <div class="body">
                            <input type="datetime" name="offTimePower" class="ft-hour" value="00:00">
                        </div>
                    </div>
                </form>
                <div class="z-btn-group">
                    <a class="z-btn z-primary" onclick="setTimerScreen()">确定</a>
                    <a class="z-btn z-default" data-popup-close>取消</a>
                </div>
            </div>
        </div>
        <!-- 移动分组 -->
        <div class="popup-move-groupCode section z-hide">
            <div class="title">移动到分组</div>
            <div class="content">
                <input type="hidden" name="deviceInfoMove" id="deviceInfoMove">
                <form class="tree-move">
                </form>
                <div class="z-btn-group" style="margin-top:30px">
                    <a class="z-btn z-primary" onclick="moveDeviceGroup()">确定</a>
                    <a class="z-btn z-default" data-popup-close>取消</a>
                </div>
            </div>
        </div>
    </div>

</div>

<script th:src="@{/lib/jquery.min.js}"></script>
<script th:src="@{/lib/layer-v3.1.1/layer/layer.js}"></script>
<script th:src="@{/js/popup.js}"></script>
<script th:src="@{/js/form.js}"></script>
<script th:src="@{/js/admin/app.js}"></script>
<script th:src="@{/lib/flatpickr/flatpickr.js}"></script>
<script th:src="@{/js/tree.js}"></script>
<script th:src="@{/js/pagination.js}"></script>
<script th:src="@{/js/prompt.js}"></script>
<script>
    //获取context path
    var context = $("meta[name='ctx']").attr("content");

    // 查询条件回显
    var pageNum = [[${pageNum}]];
    var pageSize = [[${pageSize}]];
    var total = [[${total}]];
    var groupId = '[[${groupId}]]'; // 分组Id
    var deviceCode = '[[${deviceCode}]]';// 终端机器码
    var deviceMac = '[[${deviceMac}]]';// 终端mac
    var deviceName = '[[${deviceName}]]';// 终端mac
    var deviceStatus = '[[${deviceStatus}]]';  // 终端状态
    var pagination = new Pagination(".pagination", {
        pageIndex: pageNum, // 当前页数
        pageSize: pageSize, // 每页数量
        count: total, // 总条数
        maxButtonCount: 5, // 分页按钮数量(可选)
        // 分页切换事件
        onPageChanged: function (pageIndex) {
            var url = context + "/cpDeviceInfo/listByPage";
            var paramStr = createParamStr(pageIndex, pageSize, groupId);
            if (paramStr !== "") {
                paramStr = paramStr.substring(1);
                url = url + "?" + paramStr;
            }
            window.location.href = url;
        }
    })

    function queryDeviceInfo() {
        var url = context + "/cpDeviceInfo/listByPage";
        var paramStr = createParamStr(pageNum, pageSize, groupId);
        if (paramStr !== "") {
            paramStr = paramStr.substring(1);
            url = url + "?" + paramStr;
        }
        window.location.href = url;
    }

    function checkVolume(i) {
        var volume = $(i).val();
        if (isNaN(volume)) {
            popup.tipsTop("输入格式不正确", "warning");
            $(i).val("");
            return false;
        } else if (volume < 0 || volume > 100) {
            popup.tipsTop("输入格式不正确，请输入0~100", "warning");
            $(i).val("");
            return false;
        }
    }

    // 设置为中文
    flatpickr.localize(flatpickr.l10ns.zh);
    flatpickr(".ft-hour", {
        enableTime: true,
        noCalendar: true,
        dateFormat: "H:i",
        time_24hr: true
    });

    /**
     * 创建参数字符串
     * @param pageIndex 当前页
     * @param pageSize 每页条数
     * @returns {string} 参数字符串
     */
    function createParamStr(pageIndex, pageSize, groupId) {
        var deviceMac = $("input[name='deviceMac']").val();
        var deviceName = $("input[name='deviceName']").val();
        var deviceCode = $("input[name='deviceCode']").val();
        var deviceStatus = $("input[name='deviceStatus']").val();
        var url = "";
        if (!isBlank(pageIndex)) {
            url = url + "&pageNum=" + pageIndex;
        }
        if (!isBlank(pageSize)) {
            url = url + "&pageSize=" + pageSize;
        }
        if (!isBlank(deviceMac)) {
            url = url + "&deviceMac=" + deviceMac;
        }
        if (!isBlank(deviceName)) {
            url = url + "&deviceName=" + deviceName;
        }
        if (!isBlank(groupId)) {
            url = url + "&groupId=" + groupId;
        }
        if (!isBlank(deviceCode)) {
            url = url + "&deviceCode=" + deviceCode;
        }
        if (!isBlank(deviceStatus)) {
            url = url + "&deviceStatus=" + deviceStatus;
        }
        return url;
    }

    //检查参数
    function isBlank(param) {
        if (undefined === param) {
            return true;
        }
        if (null == param) {
            return true;
        }
        if ("" === param) {
            return true;
        }
        return false;
    }

    var tree = null;
    // 当前所选终端分组
    var deviceGroup;
    // 移动分组里的所选终端分组
    var deviceGroup1;
    // 树形组件
    var tree;
    // 移动分组里的树形组件
    var tree1;

    $(function () {
        eventEmit();

        // 初始化分组tree
        $.ajax({
            type: 'post',
            dataType: 'json',
            url: context + '/deviceGroup/listDeviceGroupTree',
            data: {},
            cache: false,
            async: true,
            success: function (result) {
                data = result.data;
                initTree(data)
            },
            error: function () {
                var treedata = [{
                    label: '全部分组',
                },]
            }
        });
    })

    function eventEmit() {

        // 设备 - 按钮
        $(document).on('click', '.j-btn-control', function () {
            var state = $(this).data('state');
            var count = 0;
            var deviceMac = ""; // 终端mac地址
            var deviceCode = ""; // 终端code
            $('tbody [type="checkbox"]').each(function (index, element) {
                if (element.checked == true) {
                    var data = $(element).parents('tr')[0].dataset;
                    if (count == 0) {
                        deviceMac = data.deviceMac;
                        deviceCode = data.deviceCode;
                    } else {
                        deviceMac = deviceMac + "," + data.deviceMac;
                        deviceCode = deviceCode + "," + data.deviceCode;
                    }
                    count++;
                }
            })

            if (count == 0) {
                popup.tipsTop("请先选择数据", 'warning');
                return false;
            }

            switch (Number(state)) {
                case 0: //音量
                    popup.open(".popup-device-sound")
                    // 表单复制
                    $(".popup-device-sound").formLoad({
                        deviceCodeSound: deviceCode
                    });
                    break;
                case 1: //重启
                    restart(deviceCode, "", deviceCode);
                    break;
                case 2: //定时开关屏
                    popup.open('.popup-screen-power');

                    // 表单复制
                    $(".popup-screen-power").formLoad({
                        deviceCodePower: deviceCode
                    });

                    break;
                case 3: //设置定时开关机
                    popup.open(".popup-device-power");
                    // 表单复制
                    $(".popup-device-power").formLoad({
                        deviceCodePower: deviceCode
                    });
                    break;
                case 4: //远程关机
                    shutdown(deviceCode, "", "是否强制关闭设备" + deviceCode);
                    break;
                case 5: // 移动分组
                    popup.open(".popup-move-groupCode")
                    // 表单复制
                    $(".popup-move-groupCode").formLoad({
                        deviceInfoMove: getSelectedDevice()
                    });
                    break
                case 6: // 解绑终端
                    if (isBlank(deviceCode)) {
                        popup.tipsTop("解绑失败，无终端序列号", "warning");
                        return;
                    }
                    untie(deviceCode);
                    break
            }

        })


        // 分组 - 新增
        $('.device-group').on('click', '.btn-add', function () {
            if (groupCheckboxVerify()) {
                if (deviceGroup.deviceGroupId == -2) {
                    popup.tipsTop("未分组为系统分组，不能添加子分组", "warning");
                    return;
                }
                $('.popup-group-add').formLoad({
                    parentId: deviceGroup.deviceGroupId,
                    parentName: deviceGroup.label
                });
                popup.open(".popup-group-add")
            }
        })

        // 分组 - 编辑
        $('.device-group').on('click', '.btn-edit', function () {
            if (groupCheckboxVerify()) {
                if (deviceGroup.deviceGroupId == -2) {
                    popup.tipsTop("未分组为系统分组，不能编辑", "warning");
                    return;
                } else if (deviceGroup.deviceGroupId == -1) {
                    popup.tipsTop("全部分组为系统分组，不能编辑", "warning");
                    return;
                } else {
                    $('.popup-group-edit').formLoad({
                        deviceGroupIdEdit: deviceGroup.deviceGroupId,
                        groupNameEdit: deviceGroup.label,
                        groupCodeEdit: deviceGroup.groupCode,
                        parentIdEdit: deviceGroup.pid
                    })
                    // 弹出分组编辑
                    popup.open(".popup-group-edit")
                }
            }
        })

        // 分组 - 删除
        $('.device-group').on('click', '.btn-del', function () {
            if (groupCheckboxVerify()) {
                if (deviceGroup.deviceGroupId == -2) {
                    popup.tipsTop("未分组为系统分组，不能删除", "warning");
                    return;
                } else if (deviceGroup.deviceGroupId == -1) {
                    popup.tipsTop("全部分组为系统分组，不能删除", "warning");
                    return;
                }

                myConfirm('确认是否删除', function () {
                    deleteDeviceGroup(deviceGroup);
                });
            }
        })
    }

    // 分组选择验证
    function groupCheckboxVerify(params) {
        if (!deviceGroup) {
            popup.tipsTop("请先选择分组", 'warning');
            return false;
        }
        return true;
    }

    /**
     * 重启
     * @param deviceCodes 所选终端的code
     */
    function restart(deviceCodes, groupIds, deviceCode) {
        var msg = "是否重启设备";
		myConfirm(msg+deviceCode,function(){
			$.ajax({
                type: 'post',
                dataType: 'json',
                url: context + '/cpDeviceInfo/restart',
                data: {deviceCodes: deviceCodes, groupIds: groupIds},
                cache: false,
                async: true,
                success: function (data) {
					popup.tipsTop(data.msg,"success");
                    //刷新当前页面
					setTimeout(function(){
						window.location.href = context + "/cpDeviceInfo/listByPage";
					},1000);
                }
            });
		})
    }

    /**
     * 解绑终端
     * @param deviceCode 所选终端序列号
     */
    function untie(deviceCode) {
        var msg = "是否解绑终端：";
		myConfirm(msg+deviceCode,function(){
			$.ajax({
                type: 'post',
                dataType: 'json',
                url: context + '/cpDeviceInfo/untie',
                data: {deviceCodes: deviceCode},
                cache: false,
                async: true,
                success: function (data) {
					popup.tipsTop(data.msg,"success");
                    //刷新当前页面
					setTimeout(function(){
						window.location.href = context + "/cpDeviceInfo/listByPage";
					},1000);
                }
            })
		});
    }

    /**
     * 定时开关机设置
     */
    function setTimerShutDownOrStart() {
        var deviceCodePower = $('.popup-device-power input[name=deviceCodePower]').val();//终端code
        var bootTimePower = $('.popup-device-power input[name=bootTimePower]').val(); //开机时间
        var offTimePower = $('.popup-device-power input[name=offTimePower]').val();//关机时间
        if ((deviceCodePower == null || deviceCodePower == '') && groupCheckboxVerify()) {
            var groupIds = "";//分组编码
            for (var i = 0; i < tree.checked.length; i++) {
                var item = tree.checked[i];
                groupIds = groupIds + "," + item.deviceGroupId;
            }
            if (groupIds.substr(0, 1) == ',') {
                groupIds = groupIds.substr(1);
            }
        }
        $.ajax({
            type: 'post',
            dataType: 'json',
            url: context + '/cpDeviceInfo/setTimerShutDownOrStart',
            data: {deviceCodes: deviceCodePower, bootTime: bootTimePower, offTime: offTimePower, groupIds: groupIds},
            cache: false,
            async: true,
            success: function (data) {
                if (data.code == 0) { //操作成功，刷新当前页面
                    popup.tipsTop(data.msg,"success");
                    setTimeout(function () {
                        window.location.href = context + "/cpDeviceInfo/listByPage";
                    },1000);
                }
            }
        });
    }

    /**
     * 定时开关屏幕
     */
    function setTimerScreen() {
        var deviceCodePower = $('.popup-screen-power input[name=deviceCodePower]').val();//终端code
        var bootTimePower = $('.popup-screen-power input[name=bootTimePower]').val(); //开屏时间
        var offTimePower = $('.popup-screen-power input[name=offTimePower]').val();//关屏时间
        if ((deviceCodePower == null || deviceCodePower == '') && groupCheckboxVerify()) {
            var groupIds = "";//分组编码
            for (var i = 0; i < tree.checked.length; i++) {
                var item = tree.checked[i];
                groupIds = groupIds + "," + item.deviceGroupId;
            }
            if (groupIds.substr(0, 1) == ',') {
                groupIds = groupIds.substr(1);
            }
        }
        // if (bootTimePower > offTimePower) {
        //     alert('开机时间应不晚于关机时间');
        //     return;
        // }
        $.ajax({
            type: 'post',
            dataType: 'json',
            url: context + '/cpDeviceInfo/setScreeenTimerShutDownOrStart',
            data: {deviceCodes: deviceCodePower, bootTime: bootTimePower, offTime: offTimePower, groupIds: groupIds},
            cache: false,
            async: true,
            success: function (data) {
                if (data.code == 0) { //操作成功，刷新当前页面
                    popup.tipsTop(data.msg,"success");
                    setTimeout(function () {
                        window.location.href = context + "/cpDeviceInfo/listByPage";
                    },1000);
                }
            }
        });
    }

    /**
     * 设置音量
     */
    function setVolume() {
        var deviceCodes = $('#deviceCodeSound').val(); //终端code
        var deviceVolume = $('#deviceVolume').val(); //音量
        var groupIds = "";//分组编码
        if ((deviceCodes == null || deviceCodes == '') && groupCheckboxVerify()) {
            for (var i = 0; i < tree.checked.length; i++) {
                var item = tree.checked[i];
                groupIds = groupIds + "," + item.deviceGroupId;
            }
            if (groupIds.substr(0, 1) == ',') {
                groupIds = groupIds.substr(1);
            }
        }
        $.ajax({
            type: 'post',
            dataType: 'json',
            url: context + '/cpDeviceInfo/setVolume',
            data: {deviceCodes: deviceCodes, deviceVolume: deviceVolume, groupIds: groupIds},
            cache: false,
            async: true,
            success: function (data) {
                if (data.code == 0) { //操作成功，刷新当前页面
                    popup.tipsTop(data.msg,"success");
                    setTimeout(function () {
                        window.location.href = context + "/cpDeviceInfo/listByPage";
                    },1000);
                }
            }
        });
    }

    /**
     * 远程关机
     */
    function shutdown(deviceCodes, groupIds, msg) {
        myConfirm(msg,function () {
            $.ajax({
                type: 'post',
                dataType: 'json',
                url: context + '/cpDeviceInfo/shutdown',
                data: {deviceCodes: deviceCodes, groupIds: groupIds},
                cache: false,
                async: true,
                success: function (data) {
                    popup.tipsTop(data.msg,"success");
                    //刷新当前页面
                    setTimeout(function () {
                        window.location.href = context + "/cpDeviceInfo/listByPage";
                    },1000);
                }
            });
        })
    }

    /**
     * 新增终端分组
     */
    function saveDeviceGroup() {
        var parentId = $('#parentId').val();    //父分组
        var groupName = $('#groupName').val();  //分组名称
        var groupCode = $('#groupCode').val();  //分组编码
        $.ajax({
            type: "POST",
            url: context + "/deviceGroup/saveDeviceGroup",
            data: {deviceGroupId: "", parentId: parentId, groupName: groupName, groupCode: groupCode, updateTime: ""},
            dataType: "json",
            success: function (result) {
                if (result.code == 0) {
                    popup.tipsTop(result.msg,"success");
                    setTimeout(function () {
                        window.location.href = context + "/cpDeviceInfo/listByPage";
                    }, 1000);
                }
            }
        });
    }

    /**
     * 更新终端分组
     */
    function updateDeviceGroup() {
        var deviceGroupId = $('#deviceGroupIdEdit').val();
        var parentId = $('#parentIdEdit').val();
        var groupName = $('#groupNameEdit').val();
        var groupCode = $('#groupCodeEdit').val();
        $.ajax({
            type: "POST",
            url: context + "/deviceGroup/updateDeviceGroup",
            data: {
                deviceGroupId: deviceGroupId,
                parentId: parentId,
                groupName: groupName,
                groupCode: groupCode,
                updateTime: ""
            },
            dataType: "json",
            success: function (result) {
                if (result.code == 0) {
                    popup.tipsTop(result.data,"success");
                    setTimeout(function () {
                        window.location.href = context + "/cpDeviceInfo/listByPage";
                    },1000);
                }
            }
        });
    }

    /**
     * 删除终端分组
     */
    function deleteDeviceGroup(deviceGroup) {
        var deviceGroupId = deviceGroup.deviceGroupId;
        $.ajax({
            type: "POST",
            url: context + "/deviceGroup/deleteDeviceGroup",
            data: {deviceGroupId: deviceGroupId},
            dataType: "json",
            success: function (result) {
                popup.tipsTop(result.msg,"success");
                if (result.code == 0) {
                    setTimeout(function () {
                        window.location.href = context + "/cpDeviceInfo/listByPage";
                    },1000)
                }
            }
        });
    }

    //终端分组远程关机
    function openShutdown() {
        if (groupCheckboxVerify()) {
            var groupIds = "";
            for (var i = 0; i < tree.checked.length; i++) {
                var item = tree.checked[i];
                groupIds = groupIds + "," + item.deviceGroupId;
            }
            if (groupIds.substr(0, 1) == ',') {
                groupIds = groupIds.substr(1);
            }
            shutdown("", groupIds, "是否强制关闭该分组下的设备");
        }
    }

    //终端分组重启
    function openRestart() {
        if (groupCheckboxVerify()) {
            var groupIds = "";
            for (var i = 0; i < tree.checked.length; i++) {
                var item = tree.checked[i];
                groupIds = groupIds + "," + item.deviceGroupId;
            }
            if (groupIds.substr(0, 1) == ',') {
                groupIds = groupIds.substr(1);
            }
            restart("", groupIds, "");
        }
    }

    /**
     * 终端分组打开定时开关机设置页面
     */
    function openDevicePower() {
        if (groupCheckboxVerify()) {
            popup.open(".popup-device-power");
        }
    }

    /**
     * 终端分组打开定时开关机设置页面
     */
    function openDeviceScreenPower() {
        if (groupCheckboxVerify()) {
            popup.open(".popup-screen-power");
        }
    }

    /**
     * 终端分组打开音量设置页面
     */
    function openDeviceSound() {
        if (groupCheckboxVerify()) {
            popup.open(".popup-device-sound");
        }
    }

    // 分组选择验证
    function groupCheckboxVerify() {
        if (isBlank(tree) || isBlank(tree.checked)) {
            popup.tipsTop("请先选择分组", 'warning');
            return false;
        }
        if (tree.checked.length < 1) {
            popup.tipsTop("请先选择分组", 'warning');
            return false;
        }
        return true;
    }

    /**
     * 移动终端到分组
     */
    function moveDeviceGroup() {
        if (groupCheckboxVerify()) {
            var deviceGroupId = deviceGroup1.deviceGroupId;  // 分组编码
            var deviceInfoMove = $("#deviceInfoMove").val();    // 终端code(逗号隔开)
            $.ajax({
                type: "POST",
                url: context + "/deviceGroup/moveDeviceGroup",
                data: {deviceGroupId: deviceGroupId, deviceCodes: deviceInfoMove},
                dataType: "json",
                success: function (result) {
                    if (result.code == 0) {
                        popup.tipsTop(result.msg,"success");
                        setTimeout(function () {
                            window.location.href = context + "/cpDeviceInfo/listByPage";
                        },1000);
                    }
                }
            });
        }
    }

    function initTree(treedata) {
        // 树形控件
        tree = new Tree('.ztree', {
            type: 1,
            data: treedata,
            multiple: false,
            // 当前选中的数据
            onChange: function (trreItem) {
                if (trreItem.checked) {
                    deviceGroup = trreItem;

                } else {
                    deviceGroup = null;
                }

                // 全部分组全选/反选
                if (trreItem.label == '全部分组') {
                    tree.forEach(function (item) {
                        if (trreItem.label == '全部分组') {
                            item.checked = trreItem.checked;
                        }
                    })
                } else {
                    // 判断子类分组是否选中，修改全部分组对应状态
                    var checkAll = true;

                    tree.forEach(function (item) {
                        if (item.label != '全部分组' && !item.checked) {
                            checkAll = false;
                            return false;
                        }
                    })

                    tree.options.data[0].checked = checkAll;
                }
            },
            // 当前选中标签
            onClick: function (trreItem) {
                console.log(trreItem);
                var url = context + "/cpDeviceInfo/listByPage";
                var paramStr = createParamStr(pageNum, pageSize, trreItem.deviceGroupId);
                if (paramStr !== "") {
                    paramStr = paramStr.substring(1);
                    url = url + "?" + paramStr;
                }
                window.location.href = (url);
            }
        });

        // 移动分组页面上的树形控件
        tree1 = new Tree('.tree-move', {
            type: 1,
            data: treedata,
            multiple: false,

            // 当前选中的数据
            onChange: function (trreItem) {
                if (trreItem.checked) {
                    deviceGroup1 = trreItem;
                } else {
                    deviceGroup1 = null;
                }
            }

        })

    }

    // 从URL加载搜索数据
    $(".filter").formLoad(App.query);

    // 获取表格中选择的设备Code
    function getSelectedDevice() {
        let temp = [];
        $('tbody [type="checkbox"]').each(function (index, element) {
            if (element.checked == true) {
                temp.push($(this).parents('tr,.tr').data('deviceCode'));
            }
        })

        return temp.join(',');
    }

</script>
</body>
</html>